package com.dao;

import com.beans.GoodsInfo;
import com.beans.PageInfo;
import com.jdbc.DBUtil;
import com.utils.StrUtil;

import java.util.List;

public class GoodsDao {
    public int getGoodsCount(int bigCateId, int smallCateId, String goodsName) {
        String sql= "select count(*) from goodsInfo where 1=1 " ;

        if(bigCateId!=0){
            sql+=" and bigCateId="+bigCateId;
        }

        if(bigCateId!=0){
            sql+=" and smallCateId="+smallCateId;
        }

        if(!StrUtil.isNullOrEmpty(goodsName)){
            sql+=" and goodsName like '%"+goodsName+"%'";
        }

        return  Integer.parseInt(DBUtil.getScalar(sql)+"");
    }

    public List<GoodsInfo> getGoodsList(int bigCateId, int smallCateId, String goodsName, PageInfo pageInfo) {
        String sql= " select temp.*,b.cateName as smallCateName from "
                + "(select a.*,b.cateName as bigCateName from goodsInfo a left join cateInfo b on a.bigcateId=b.id) temp "
                + " left join cateInfo b on temp.smallcateId=b.id  where 1=1 " ;

        if(bigCateId!=0){
            sql+=" and bigCateId="+bigCateId;
        }

        if(bigCateId!=0){
            sql+=" and smallCateId="+smallCateId;
        }

        if(!StrUtil.isNullOrEmpty(goodsName)){
            sql+=" and goodsName like '%"+goodsName+"%'";
        }

        sql+=" order by editDate desc limit ?,?";

        return DBUtil.getList(sql, GoodsInfo.class, pageInfo.getBeginRow(),pageInfo.getPageSize());
    }

    public int delGoodsById(Integer goodsId) {
        String sql="delete from goodsInfo where id=?";
        return DBUtil.update(sql,goodsId);
    }

    public void delAdmin(String[] ids) {
        String sql = "delete from adminInfo  where id in(";
        for(int i=0;i<ids.length;i++) {
            if(i<ids.length-1) {
                sql+=ids[i] + ",";
            }else {
                sql+=ids[i] + ")";
            }
        }
        DBUtil.update(sql);
    }

    public GoodsInfo getGoodsById(int goodsId) {
        String sql="select * from goodsInfo where id=?" ;
        return DBUtil.getSingleObject(sql, GoodsInfo.class, goodsId);
    }

    public int addGoods(GoodsInfo goods) {
        String sql="insert into goodsInfo (goodsName,bigCateId,smallCateId,unit,price,producter,des,pictureData)  values (?,?,?,?,?,?,?,?)";
        Object [] params={
                goods.getGoodsName(),
                goods.getBigCateId(),
                goods.getSmallCateId(),
                goods.getUnit(),
                goods.getPrice(),
                goods.getProducter(),
                goods.getDes(),
                goods.getPictureData()
        };
        return DBUtil.update(sql, params);
    }

    public int updateGoods(GoodsInfo goods) {
        String sql="update goodsInfo  set goodsName=? ,unit=? ,price=?,des=?,producter=?,bigCateId=?, smallCateId=? ,pictureData=? where id=?";
        Object [] params={
                goods.getGoodsName(),
                goods.getUnit(),
                goods.getPrice(),
                goods.getDes(),
                goods.getProducter(),
                goods.getBigCateId(),
                goods.getSmallCateId(),
                goods.getPictureData(),
                goods.getId()
        };
        return DBUtil.update(sql,params);
    }
}
